
# Load Libraries
library(tidyr)
library(dplyr)
library(ggplot2)
library(ggsignif)
library(hrbrthemes)


# Read responses
mydata.img.final <- read.csv(file="data/study_2/responses.csv",head=T,sep=c(","))

# Read features
features <- read.csv(file="data/features_labels_wide.csv",head=T,sep=c(","))
truth <- features[,c("file_id","escaped")]
truth$file_id <- as.character(truth$file_id)
mydata.img.final.truth <-
  merge(mydata.img.final,
        truth,
        by.x = c("Img"),
        by.y = c("file_id"))

nrow(mydata.img.final.truth)

# Calculate accuracy manually
mydata.img.final.truth$Guessed_Right <- as.numeric(mydata.img.final.truth$Esc == mydata.img.final.truth$escaped)

# Aggregate per user
mydata.analysis <- mydata.img.final.truth %>% group_by(Condition, TurkerID) %>% summarise(c = length(Guessed_Right),m = mean(Guessed_Right))
mean(mydata.analysis[which(mydata.analysis$Condition=="control"),]$m) # 0.5824742
mean(mydata.analysis[which(mydata.analysis$Condition=="training4"),]$m) # 0.6446602
mean(mydata.analysis[which(mydata.analysis$Condition=="training8"),]$m) # 0.639604
mean(mydata.analysis[which(mydata.analysis$Condition=="training12"),]$m) # 0.6739583

summary(lm(m~Condition,data=mydata.analysis[which(mydata.analysis$Condition %in% c("control","training4")),]))
summary(lm(m~Condition,data=mydata.analysis[which(mydata.analysis$Condition %in% c("control","training12")),]))
summary(lm(m~Condition,data=mydata.analysis[which(mydata.analysis$Condition %in% c("training4","training8")),]))
summary(lm(m~Condition,data=mydata.analysis[which(mydata.analysis$Condition %in% c("training4","training12")),]))
summary(lm(m~Condition,data=mydata.analysis[which(mydata.analysis$Condition %in% c("training8","training12")),]))

lm.DiM <- lm(m~Condition,data=mydata.analysis)
summary(lm.DiM)


mydata.analysis <- mydata.img.final.truth %>% 
  group_by(Condition, TurkerID) %>% 
  summarise(
    c = length(Guessed_Right),
    m = mean(Guessed_Right)
  )


# Prepare for plotting
plotdata <- mydata.analysis %>% group_by(Condition) %>% summarise(mm = mean(m), se = sciplot::se(m))
plotdata$Condition <- c("No training", "Training 12", "Training 4", "Training 8")
plotdata <- plotdata[c(1,3,4,2),]
plotdata$Condition <- factor(plotdata$Condition,levels = unique(plotdata$Condition))
plotdata$Source <- rep("Human",4) 
plotdata.B <- plotdata
plotdata.B$lower <- plotdata.B$mm - 1.96 * plotdata.B$se
plotdata.B$upper <- plotdata.B$mm + 1.96 * plotdata.B$se

plt <- plotdata.B %>%
  ggplot(aes(x=Condition,y=mm,fill=Condition), color=Condition) +  
  stat_summary(fun.y=mean,position=position_dodge(),geom="bar",width=0.6)+
  geom_text(aes(label=round(mm,2)),size = 9, position = position_stack(vjust = 0.85),color="white")+
  geom_errorbar(aes(ymin = mm - 1.96*se, ymax=mm + 1.96*se), 
                position=position_dodge(.9), width=.2, size=1, alpha=1)+
  geom_signif(y_position=c(0.75, 0.75, 0.75, 0.87), annotation=c("**","n.s.","n.s.","***"), xmax = c(1.95,2.95,4,4),
              xmin=c(1,2.05,3.05,1) ,size=1.5,textsize=10)+
  labs(title="Study 2")+
  xlab("")+
  ylab("\nAccuracy")+
  #coord_flip()+
  ylim(0,0.95)+
  theme_bw()+
  scale_color_manual(name="", values=c("#EE0000FF","#3B499299","#3B4992CC","#3B4992FF"))+
  scale_fill_manual(name="", values=c("#EE0000FF","#3B499299","#3B4992CC","#3B4992FF"))+
  guides(fill = guide_legend(reverse = TRUE))+
  guides(color = guide_legend(reverse = TRUE))+ 
  theme_ipsum_rc(grid="Y",axis_title_just="m")+
  theme(text=element_text(size=26),legend.position="no",aspect.ratio=1/1.5,plot.title = element_text(size=28,hjust = 0.5),
        axis.text.y = element_text(size=22,colour = "black"),axis.title.y = element_text(size=26),
        axis.text.x = element_text(size=24,colour="black"),
        legend.text=element_text(size=16),strip.background = element_rect(color="black",fill="white"),
        panel.border = element_rect(colour = "black", fill=NA, size=1.5), plot.margin = unit(c(0.1, 0, 0, 0), "cm"))


print(plt)

# END